package com.situ.storehouse.dao;

import java.util.List;


import com.situ.storehouse.entity.Wares;
import com.situ.storehouse.util.Dbutil;


public class WaresDao {

	public void add(Wares wares) {
		String sql = "insert into wares(wares_name,factory,model,specifications) values(?,?,?,?)";
		Dbutil.executeDML(sql, wares.getWaresName(),wares.getFactory(),wares.getModel(),wares.getSpecifications());
	}

	public void delete(Integer id) {
		String sql = "delete from wares where id=?";
		Dbutil.executeDML(sql, id);
	}

	public void update(Wares wares) {
		String sql = "update wares set wares_name=?,factory=?,model=?,specifications=? where id=?";
		Dbutil.executeDML(sql,wares.getWaresName(),wares.getFactory(),wares.getModel(),wares.getSpecifications(),wares.getId());
	}

	public List<Wares> selectAll() {
		String sql="select * from wares";
		return Dbutil.executeDQL(sql,Wares.class);
	}
	
	public Wares select(Integer id) {
		String sql = "select * from wares where id=?";
		List<Wares> list = Dbutil.executeDQL(sql,Wares.class, id);
		return list.get(0);
	}
	
	public List<Wares> select(Integer page,Integer limit,String keyword) {
		String sql=null;
		if(keyword==null || keyword.equals("")) {
		sql = "select * from wares limit ?,?";
		}else{
			sql="select * from wares where instr(wares_name,'"+keyword+"')>0 limit ?,?";
		}
		return Dbutil.executeDQL(sql, Wares.class,page,limit);
		
	}

	public Long count(String keyword) {
		String sql=null;
		if(keyword==null || keyword.equals("")) {
			sql="select count(*) from wares";
		}else {
			sql="select count(*) from wares  where instr(wares_name,'"+keyword+"')>0 ";
		}
		
		return Dbutil.executeCount(sql);
		
	}

	

	
}
